const express = require('express')
const app = express()
const fs = require('fs')
const path = require('path')
const Mock = require('mockjs')
const { debounce } = require('lodash')
console.log('debounce:', debounce)

app.use(express.static(path.join(__dirname, '/views')))

// 数据模拟
/**
 *
 * status: 0 1 2 3
 * nickname: ''
 * phone
 */

// const cart = Mock.mock({
// 	list: {
// 		'children|5': []
// 	}
// })

const cartData = [
	{
		tabId: '1',
		count: 1,
		status: '海鲜',
		children: [
			{
				id: '0-1',
				num: 0,
				name: '111'
			},
			{
				id: '0-1',
				num: 0,
				name: '111'
			}
		]
	},
	{
		id: '2',
		count: 2,
		status: '营养'
	},
	{
		id: '3',
		count: 0,
		status: '失主'
	},
	{
		id: '4',
		count: 0,
		status: '精选'
	}
]

app.get('/api/allCount', (req, res) => {
	const allCount = cartData.reduce((num, item) => {
		return num + item.count
	}, 0)
	res.send({ code: 0, allCount })
})
// [{children:[]}, {}, {}, {}]

const mock = Mock.mock({
	'list|10': [
		{
			id: '@id',
			'status|+1': [0, 1, 2, 3],
			phone: '@city'
		}
	]
})

// fs.writeFileSync('./data.json', JSON.stringify(mock.list))

// console.log('mock', mock.list)

app.get('/api/del', async (req, res, next) => {
	console.log(req.query.ids)
	// 1
	res.send({ code: 0 })
})

app.get('/api/test', (req, res) => {
	res.send({ code: 0 })
})

// app.get('/api/del', async (req, res, next) => {
// 	console.log(req.query.ids)

// 	res.send({ code: 0 })
// })

// 0 通过 1 未通过

// 通过/拒绝   通过 拒绝

// ids = [1,2,3,3,4,4,5]
app.get('/api/operate', async (req, res, next) => {
	const { status, id } = req.query
	switch (status) {
		case '通过': //用户行为
			// const data = fs.readFileSync('./data.json')
			// data.forEach((item) => {
			// 	if (item.id === id) {
			// 		item.status = 1
			// 	}
			// })
			// const res = fs.writeFileSync('./data.json', JSON.stringify(data))

			// res.send({ code: 0, res })
			break
		case '拒绝':
			// const resdata = fs.readFileSync('./data.json')
			// resdata.forEach((item) => {
			// 	if (item.id === id) {
			// 		item.status = 0
			// 	}
			// })
			// // const resdata = fs.writeFileSync('./data.json', JSON.stringify(resdata))

			// res.send({ code: 0 })
			break
		// case '2':
		// 	res.send({ code: 0 })
		// 	break
		// case '3':
		// 	res.send({ code: 0 })
		// 	break
	}
})

app.listen(3000)
